﻿using DAO;
using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace BUS
{
    public class DienThoaiBUS
    {
        DataProvider dao = new DataProvider();
        DienThoaiDTO dto = new DienThoaiDTO();
        public DienThoaiBUS() { }
        public DataTable LayDuLieuChiTiet(int masp)
        {
            object[] para = { "@Madienthoai", masp };

            return dao.LayDuLieuThamSo("tlb_DienThoai_LoadCTSP", CommandType.StoredProcedure, para);

        }
        public DataTable LayDuLieuHinhAnhSanPham(int masp)
        {
            object[] para = { "@Madienthoai", masp };
            return dao.LayDuLieuThamSo("tbl_HinhSanPham_LoadDSHinhAnh", CommandType.StoredProcedure, para);
        }

        public DataTable LoadHotProduct()
        {
            return dao.LayDuLieuThamSo("tbl_SanPham_LoadHotProduct", CommandType.StoredProcedure);
        }
        public DataTable LoadNewProduct()
        {
            return dao.LayDuLieuThamSo("tbl_SanPham_LoadNewProduct", CommandType.StoredProcedure);
        }

        public DataTable LoadSPCunghang(int masp, int mahangsx)
        {
            object[] para = { "@Madienthoai", masp, "@MaHangSX", mahangsx };
            return dao.LayDuLieuThamSo("tbl_DienThoai_LoadSanPhamCungHang", CommandType.StoredProcedure, para);
        }
        public DienThoaiDTO LayCTDienThoai(int MaDienThoai)
        {
            DienThoaiDTO objDT = new DienThoaiDTO();
            DataTable dt = dao.LayDuLieuThamSo("tlb_DienThoai_LoadCTSP", CommandType.StoredProcedure, new object[] { "@Madienthoai", MaDienThoai });
            objDT.MaDienThoai = Convert.ToInt16(dt.Rows[0]["MaDienThoai"]);
            objDT.TenDienThoai = dt.Rows[0]["TenDienThoai"].ToString();
            objDT.Gia = Convert.ToDecimal(dt.Rows[0]["Gia"]);
            objDT.MaHangSX = Convert.ToInt16(dt.Rows[0]["MaHangSX"]);
            objDT.SoLuong = Convert.ToInt16(dt.Rows[0]["SoLuong"]);
            objDT.SoLuongDaBan = Convert.ToInt16(dt.Rows[0]["SoLuongDaBan"]);
            objDT.TrangThai = Convert.ToBoolean(dt.Rows[0]["Trangthai"]);
            objDT.NgayTao = Convert.ToDateTime(dt.Rows[0]["NgayTao"]);
            objDT.HinhDaiDien = dt.Rows[0]["HinhDaiDien"].ToString();
            objDT.TinhTrang = Convert.ToBoolean(dt.Rows[0]["TinhTrang"].ToString());
            objDT.MoTaSanPham = dt.Rows[0]["MoTaSanPham"].ToString();
            return objDT;
        }

        #region Quản lý sản phẩm
            public bool ThemSP(DienThoaiDTO objDT)
            {
                object[] para = { 
                                    "@TenDienThoai",objDT.TenDienThoai,"@SoLuong",objDT.SoLuong,"@Gia",objDT.Gia,
                                    "@TrangThai",objDT.TrangThai,"@TinhTrang",objDT.TinhTrang,"@SoLuongDaBan",objDT.SoLuongDaBan,
                                    "@MoTaSanPham",objDT.MoTaSanPham,"@MaHangSX",objDT.MaHangSX,"@NgayTao",objDT.NgayTao,"@HinhDaiDien",objDT.HinhDaiDien
                                };
                string query = "tbl_DienThoai_ThemDienThoai";
                bool kq = dao.ExecuteQuery(query, CommandType.StoredProcedure, para);
                return kq;
            }

            public DataTable LoadHangSX()
            {
                return dao.LayDuLieuThamSo("tbl_HangSX_LayDSHangSX", CommandType.StoredProcedure);
            }

            public DataTable LayDSSanPham()
            {
                return dao.LayDuLieuThamSo("tbl_SanPham_LayDSSanPham", CommandType.StoredProcedure);
            }

            public DataTable LayDuLieuSanPham(int madt)
            {
                object[] para = { "@MaDienThoai",  madt};

                return dao.LayDuLieuThamSo("tbl_SanPham_LayDuLieuSanPham", CommandType.StoredProcedure, para);
            }

            public bool CapNhatSanPham(DienThoaiDTO objDT)
            {
                object[] para = { 
                                    "@MaDienThoai",objDT.MaDienThoai,"@TenDienThoai",objDT.TenDienThoai,"@SoLuong",objDT.SoLuong,"@Gia",objDT.Gia,
                                    "@TrangThai",objDT.TrangThai,"@TinhTrang",objDT.TinhTrang,
                                    "@MoTaSanPham",objDT.MoTaSanPham,"@MaHangSX",objDT.MaHangSX,"@HinhDaiDien",objDT.HinhDaiDien
                                };
                string query = "tbl_SanPham_QuanLySanPham";
                bool kq = dao.ExecuteQuery(query, CommandType.StoredProcedure, para);
                return kq;
            }
        #endregion
    }
}